<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <div class="tab-list fl-l">
          <a-radio-group :defaultValue="queryParam.type" @change="handleTabs">
            <a-radio-button
                v-for="(item, index) in CategoryType.data"
                :key="index"
                :value="item.key"
            >{{ item.name }}
            </a-radio-button>
          </a-radio-group>
        </div>
        <div class="button-group">
          <a-button-group>
            <a-button v-action:add icon="plus" type="primary" @click="handleAdd">新增</a-button>
          </a-button-group>
        </div>
      </div>
    </div>
    <a-table
        v-if="!isLoading"
        :defaultExpandAllRows="false"
        :columns="columns"
        :dataSource="categoryList"
        rowKey="id"
        :expandIconColumnIndex="1"
        :pagination="false"
        :loading="isLoading"
    >
      <template v-slot:action="text, item">
        <a v-action:edit style="margin-right: 8px;" @click="handleEdit(item)">编辑</a>
        <a v-action:delete @click="handleDelete(item)">删除</a>
      </template>
    </a-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh"/>
    <RenameForm ref="RenameForm" :categoryList="categoryList" @handleSubmit="handleRefresh"/>
  </a-card>
</template>

<script>
import * as Api from '@/api/category'
import {STable} from '@/components'
import AddForm from './modules/AddForm'
import RenameForm from './modules/RenameForm'
import {CategoryTypeEnum} from '@/common/enum/product'

export default {
  name: 'Index',
  components: {
    STable,
    AddForm,
    RenameForm
  },
  data() {
    return {
      // 分类列表
      categoryList: [],
      // 查询参数
      queryParam: {
        type: CategoryTypeEnum.VIRTUAL.key
      },
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '分类编号',
          dataIndex: 'catNo'
        },
        {
          title: '分类名称',
          dataIndex: 'catName'
        },
        {
          title: '添加时间',
          dataIndex: 'creationTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: {customRender: 'action'}
        }
      ]
    }
  },
  computed: {
    CategoryType() {
      return CategoryTypeEnum
    }
  },
  created() {
    // 获取分类列表
    this.getCategoryList(true)
  },
  methods: {
    // 获取分类列表
    getCategoryList(bool) {
      bool && (this.isLoading = true)
      Api.list(this.queryParam)
          .then(result => {
            this.categoryList = result.items
          })
          .finally(() => this.isLoading = false)
    },

    // 新增记录
    handleAdd() {
      this.$refs.AddForm.add(this.queryParam.type)
    },

    // 编辑记录
    handleEdit(item) {
      this.$refs.RenameForm.rename(item)
    },
    // 切换tab
    handleTabs(e) {
      this.queryParam.type = e.target.value
      this.handleRefresh()
    },
    // 删除记录
    handleDelete(item) {
      const self = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleted(item['id'])
              .then(result => {
                self.$message.success("操作成功", 1.5)
                self.handleRefresh()
              })
              .finally(result => modal.destroy())
        }
      })
    },

    /**
     * 刷新列表
     * @param {Boolean} bool 强制刷新到第一页
     */
    handleRefresh(bool) {
      // 获取分类列表
      this.getCategoryList()
    }

  }
}
</script>

<style lang="less" scoped>
// 筛选tab
.tab-list {
  margin-right: 20px;
}
</style>